/**
 * 
 */

$(function() {
	$('.nav').superfish({
		hoverClass	 : 'sfHover',
		pathClass	 : 'overideThisToUse',
		delay		 : 0,
		animation	 : {height: 'show'},
		speed		 : 'normal',
		autoArrows   : false,
		dropShadows  : false, 
		disableHI	 : false, /* set to true to disable hoverIntent detection */
		onInit		 : function(){},
		onBeforeShow : function(){},
		onShow		 : function(){},
		onHide		 : function(){}
	});
	
	$('.nav').css('display', 'block');
	$( "#tabs" ).tabs();
	$("button").button();
	$( ".filter" ).button({icons: {secondary: "ui-icon-shuffle"}, text: false});
	$( ".status" ).button({icons: {primary: "ui-icon-lightbulb"}, text: false});
	
	$( ".edit" ).button({icons: {primary: "ui-icon-wrench"}, text: false})
		.next().button({icons: {primary: "ui-icon-trash"}, text: false});
	
	$('.ui-state-default').hover(
		function(){ $(this).addClass('ui-state-hover'); }, 
		function(){ $(this).removeClass('ui-state-hover'); }
	);
	
	$('#image_upload').live('click', function() {
		image_upload('image', 'preview');
	});
});

function image_upload(field, preview) {
	$('#dialog').remove();
	
	$('#body').prepend('<div id="dialog" style="padding: 3px 0px 0px 0px;"><iframe src="'+base_path+'backend/filemanager/index/' + encodeURIComponent(field) + '" style="padding:0; margin: 0; display: block; width: 100%; height: 100%;" frameborder="no" scrolling="auto"></iframe></div>');
	
	$('#dialog').dialog({
		title: 'Image Manager',
		close: function (event, ui) {
			if ($('#' + field).attr('value')) {
				$.ajax({
					url: base_path + 'backend/filemanager/image',
					type: 'POST',
					data: 'image=' + encodeURIComponent($('#' + field).attr('value')),
					dataType: 'text',
					success: function(data) {
						$('#' + preview).replaceWith('<img src="' + data + '" alt="" id="' + preview + '" class="image" onclick="image_upload(\'' + field + '\', \'' + preview + '\');" />');
					}
				});
			}
		},	
		bgiframe: false,
		width: 700,
		height: 400,
		resizable: false,
		modal: false
	});
};